package com.hetaozi.login.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * @email： wf520200@163.com
 * @date: 2021/11/11 14:57
 * 功能：
 * 描述：
 * @author： 吴帆
 **/
@RestController
@Api(tags = "系统登陆相关接口")
@RequestMapping("/system")
public class LoginController {

    @ApiOperation(value = "登陆")
    @PostMapping("login")
    public String login(String userName, String passWord){
        if ("".equals(userName.trim()) || "".equals(passWord.trim())) {
            return "账号或密码不能为空";
        }
        try {
            UsernamePasswordToken token = new UsernamePasswordToken(userName, passWord);
            Subject subject = SecurityUtils.getSubject();
            subject.login(token);

            // 处理菜单

            return "登录成功";
        } catch (IncorrectCredentialsException ice) {
            return "密码错误";
        } catch (UnknownAccountException uae) {
            return "账号不存在";
        } catch (LockedAccountException e) {
            return "账号被锁定";
        } catch (ExcessiveAttemptsException eae) {
            return "操作频繁，请稍后再试";
        }
    }


    @ApiOperation(value = "登出")
    @GetMapping("loginOut")
    public String loginOut(){
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return "登出";
    }

}
